<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 随机类文件
 * +--------------------------------------------------------------
 */

namespace luo\traits;


trait Rand
{

    private static $en   = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    private static $num  = '0123456789';
    private static $char = '_';

    /**
     * 生成指定长度的混合随机内容
     * @param int $len
     * @return string
     */
    public static function mixture($len = 4){

        $string = implode('', [self::$en, self::$num, self::$char]);
        return self::rand($string, $len);

    }

    /**
     * 生成指定长度的随机字符串
     * @param int $len
     * @return string
     */
    public static function string($len = 4){

        return self::rand(self::$en, $len);

    }

    /**
     * 生成指定长度的随机数字
     * @param int $len
     * @return int
     */
    public static function number($len = 4){

        return self::rand(self::$num, $len);

    }

    /**
     * 生成指定长度的随机内容
     * @param $str
     * @param int $len
     * @return string
     */
    private static function rand($str, $len = 4){

        $string = '';
        $i = 0;
        while ($i < $len){
            $rand = mt_rand(0, (strlen($str) - 1));
            $string .= substr($str, $rand, 1);
            $i++;
        }
        return $string;

    }

}